<template>
  <div class="order">
    <div class="order-opera">
      <van-button type="default" size="mini" @click="useOperatorBoxStore().showOverlay(false, '')">取消</van-button>
      <span>排序选择</span>
      <van-button type="primary" size="mini" @click="handleConfirm">完成</van-button>
    </div>
    <div class="order-content">
      <div
        class="order-item"
        :class="{ active: index === currentIndex }"
        v-for="(item, index) in orders"
        :key="index"
        @click="orderClick(index)"
      >
        <van-space>
          <i class="iconfont" :class="item.iconName"></i>
          <span>{{ item.orderName }}</span>
        </van-space>
        <span
          v-if="index === currentIndex"
          @click.stop="orderItemClick(index)"
          >{{ item.orderOption[currentOrderItem] }}</span
        >
      </div>
    </div>
  </div>
</template>

<script setup>
// 作者：YCH
// 时间：2022年10月31日 22:21:14
import { ref } from "vue";
import useArticleFilterStore from "@/stores/modules/articleFilter.js";
import useOperatorBoxStore from "@/stores/modules/operatorBox.js";
// 当前排序选项
const currentIndex = ref(0);
// 当前排序选项子项 从新到旧 A-Z
const currentOrderItem = ref(0);

const emit = defineEmits(["handleOrderConfirm"]);

const orders = [
  {
    iconName: "icon-chuangjianshijian",
    orderName: "按创建日期",
    orderOption: ["从新到旧", "从旧到新"],
  },
  {
    iconName: "icon-gengxinshijian",
    orderName: "按更新日期",
    orderOption: ["从新到旧", "从旧到新"],
  },
  {
    iconName: "icon-zimupaixu",
    orderName: "按标题",
    orderOption: ["A-Z", "Z-A"],
  },
  {
    iconName: "icon-Web",
    orderName: "按来源",
    orderOption: ["A-Z", "Z-A"],
  },
];

/** 参数一： 当前排序选项 */
const orderClick = (index) => {
  currentIndex.value = index;
  // 重置选选项
  currentOrderItem.value = 0;
  useArticleFilterStore().currentOrders.iconName = orders[index].iconName;
  useArticleFilterStore().queryParams.orderField = getOrderField(orders[index].orderName);
  useArticleFilterStore().queryParams.orderType = 'ASC';
  useArticleFilterStore().currentOrders.orderItem = orders[index].orderOption[currentOrderItem.value];
};

/** 参数一： 当前排序选项 参数二：当前排序选项子项 */
const orderItemClick = (index) => {
  if (currentOrderItem.value == 0) {
    currentOrderItem.value = 1;
    useArticleFilterStore().queryParams.orderType = 'DESC';
    useArticleFilterStore().currentOrders.orderItem = orders[index].orderOption[currentOrderItem.value];
  } else {
    currentOrderItem.value = 0;
    useArticleFilterStore().queryParams.orderType = 'ASC';
    useArticleFilterStore().currentOrders.orderItem = orders[index].orderOption[currentOrderItem.value];
  }
};

/** 点击事件 */
const handleConfirm = () => {
  emit("handleOrderConfirm");
}

/**
 * 根据val获取OrderField
 * @returns {string}
 */
const getOrderField = (val) =>{
  if(val == '按更新日期'){
    return 'updateTime'
  }else if(val == '按标题'){
    return 'articleTitle'
  }else if(val == '按来源'){
    return 'origin'
  }else {
    return 'createTime'
  }
}
</script>

<style lang="less" scoped>
.order {
  padding: 10px 12px;
  .order-opera {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .order-content {
    margin-top: 10px;
    padding: 10px;
    border-radius: 8px;
    color: var(--primary-font-color);
    font-family: var(--primary-font-family);
    background-color: var(--primary-background-color);
    .order-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 34px;
      font-size: 16px;
    }
    .active {
      color: #4c9bf5;
    }
  }
}
</style>
